import { getAcidDrumEquipStatus, getSpecialGasEquipStatus } from "@/api/modules/operation";
import { queryEquipSystem } from "@/api/equip/archive";
import dayjs from "dayjs";
import { formatMissingData } from "@/utils/common";

/**
 * @description:  获取search配置信息
 * @param {string} code
 * @param {number} flag
 * @return {*}
 */
export const useSearchOption = (code: string, flag?: number): Array<CommonSearch.SearchOption> => {
  const systemCode: CommonSearch.SearchOption = {
    label: "子系统",
    key: "systemCode",
    value: "",
    type: "select",
    axiosApi: queryEquipSystem,
    params: { className: "GAS" },
    formatOption: (list: Array<any>) =>
      list
        ?.find(item => item.code === code)
        ?.children?.map(({ name: label = null, code: value = null }) => ({ label, value }))
        .filter(
          item =>
            item.label !== "废排系统" &&
            item.label !== "网络系统" &&
            item.label !== "电力系统" &&
            item.label !== "辅助系统" &&
            item.label !== "Gas-Hookup"
        ),
    props: { filterable: true },
    placeholder: "请选择子系统",
    span: 5
  };

  const deviceCode: CommonSearch.SearchOption = {
    label: "设备编号",
    key: "deviceCode",
    value: "",
    type: "input",
    placeholder: "请输入设备编号",
    span: 5
  };

  const deviceStatus: CommonSearch.SearchOption = {
    label: "状态",
    key: "deviceStatus",
    value: "",
    type: "select",
    axiosApi: flag === 1 ? getAcidDrumEquipStatus : flag === 2 ? getSpecialGasEquipStatus : null,
    mapOption: ({ code: value = "", name: label = "" }: any) => ({ value, label }),
    placeholder: "请选择状态",
    span: 5
  };
  if (flag) {
    return [systemCode, deviceCode, deviceStatus];
  } else {
    return [systemCode, deviceCode];
  }
};

// /**
//  * @description: formatAcidDrumEquipBackgroundImageUrl
//  * @param {number} capacity
//  * @return {*}
//  */
// export const formatAcidDrumEquipBackgroundImageUrl = (capacity: number) => {
//   if (capacity === 0) {
//     return "empty";
//   } else if (capacity > 0) {
//     return "run";
//   } else {
//     return "default";
//   }
// };

/**
 * @description: formatAcidDrumEquipBackgroundImageUrl
 * @param {string} status  RUN 运行   DOWN 停止   NULL 桶空
 * @return {*}
 */
export const formatAcidDrumEquipBackgroundImageUrl = (status: string) => {
  switch (status) {
    case "RUN":
      return "run";
    case "DOWN":
      return "default";
    case "NULL":
      return "empty";
    default:
      return "default";
  }
};

/**
 * @description: formatSpecialGasEquipBackgroundImageUrl
 * @param {number} capacity
 * @return {*}
 */
export const formatSpecialGasEquipBackgroundImageUrl = (capacity: number) => {
  if (capacity === 0) {
    return "0";
  } else if (capacity > 0 && capacity <= 20) {
    return "10";
  } else if (capacity > 20 && capacity <= 40) {
    return "25";
  } else if (capacity > 40 && capacity <= 60) {
    return "50";
  } else if (capacity > 60 && capacity <= 80) {
    return "75";
  } else if (capacity > 80 && capacity < 100) {
    return "90";
  } else if (capacity === 100) {
    return "100";
  } else {
    return "default";
  }
};

/**
 * @description: formatChemicalEquipBackgroundImageUrl
 * @param {number} capacity
 * @return {*}
 */
export const formatChemicalEquipBackgroundImageUrl = (capacity: number) => {
  if (capacity === 0) {
    return "0";
  } else if (capacity > 0 && capacity < 20) {
    return "10";
  } else if (capacity >= 20 && capacity < 40) {
    return "25";
  } else if (capacity >= 40 && capacity < 60) {
    return "50";
  } else if (capacity >= 60 && capacity <= 80) {
    return "75";
  } else if (capacity > 80 && capacity <= 100) {
    return "100";
  } else {
    return "default";
  }
};

/**
 * @description: useInitEchartsOptions
 * @param {Array} list
 * @param {string} name
 * @param {string} unit
 * @param {number} scale
 * @return {*}
 */
export const useInitEchartsOptions = (list: Array<any>, name: string, unit: string, scale: number) => {
  if (!list?.length) {
    return null;
  }
  const tooltip = {
    trigger: "axis",
    formatter: (params: any) => {
      // const { seriesName, name, value } = params;
      // return `${seriesName}<br/>${name}：${value.toFixed(scale)}`;

      const { seriesName, name, value } = params;
      const arr = params.map(item => {
        const { seriesName, value } = item;
        return `${seriesName}：${value?.toFixed(scale)}`;
      });
      arr.unshift(params[0]?.axisValue);
      return arr.join("<br/>");
    }
  };
  const grid = {
    left: 60,
    right: 50,
    top: 40,
    bottom: 60,
    containLabel: true
  };
  const xAxisData = list.map(item => dayjs(item.day).format("MM/DD HH:mm"));
  const seriesData = formatMissingData(list.map(item => item.meterReadingVal));
  const xAxis = {
    type: "category",
    boundaryGap: false,
    name: "日期",
    data: xAxisData
  };
  const min = value => {
    if (value.min < 0) {
      return Math.round(value.min * 1.5);
    } else {
      return 0;
    }
  };
  const max = value => {
    return Math.round(value.max * 1.5);
  };
  const yAxis = {
    type: "value",
    name: unit ? `单位/${unit}` : undefined,
    min,
    max
  };
  const dataZoom = [
    {
      type: "slider",
      orient: "vertical",
      filterMode: "none",
      left: 10,
      start: 0,
      end: 100
    },
    {
      type: "inside",
      orient: "vertical",
      filterMode: "none",
      start: 0,
      end: 100
    },
    {
      type: "slider",
      orient: "horizontal",
      filterMode: "none",
      start: 0,
      end: 100
    },
    {
      type: "inside",
      orient: "horizontal",
      filterMode: "none",
      start: 0,
      end: 100
    }
  ];
  const series = [
    {
      type: "line",
      symbolSize: 6,
      name,
      data: seriesData,
      color: "#4786FF"
    }
  ];
  return { tooltip, grid, xAxis, yAxis, dataZoom, series };
};

export const colors = new Map([
  [
    "OFFLINE",
    {
      label: "下线",
      // color: "#EF4444",
      // backgroundColor: "#FBD0D0",
      // borderBackgroundColor: "#F8AEAE"
      color: "#888",
      backgroundColor: "#e6e6e6",
      borderBackgroundColor: "#ccc"
    }
  ],
  [
    "IN_STANDBY_MODE",
    {
      label: "待机中",
      // color: "#909399",
      // backgroundColor: "#E6E7E8",
      // borderBackgroundColor: "#D2D4D5"
      color: "#10B981",
      backgroundColor: "#C5EEE0",
      borderBackgroundColor: "#9BE2CA"
    }
  ],
  [
    "DURING_ASPIRATION",
    {
      label: "送气中",
      color: "#10B981",
      backgroundColor: "#C5EEE0",
      borderBackgroundColor: "#9BE2CA"
    }
  ],
  [
    "PURGE_COMPLETED",
    {
      label: "吹扫完成",
      color: "#F59E0B",
      backgroundColor: "#FDE7C3",
      borderBackgroundColor: "#FAD494"
    }
  ],
  [
    "NULL",
    {
      label: "桶空",
      color: "#EF4444",
      backgroundColor: "#FBD0D0",
      borderBackgroundColor: "#F8AEAE"
    }
  ],
  [
    "RUN",
    {
      label: "运行",
      color: "#10B981",
      backgroundColor: "#C5EEE0",
      borderBackgroundColor: "#9BE2CA"
    }
  ],
  [
    "DOWN",
    {
      label: "吹扫完成",
      color: "#F59E0B",
      backgroundColor: "#FDE7C3",
      borderBackgroundColor: "#FAD494"
    }
  ]
]);
